<template>
  <div class="register-page">
    <el-card class="register-card">
      <template #header>
        后台管理系统
        <div @click="$router.push('/login')">去登录</div>
      </template>
      <el-form ref="regRef" :model="form" :rules="rules">
        <el-form-item label="用户名：" prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="密码：" prop="password">
          <el-input v-model="form.password" type="password" placeholder="请输入用户密码"></el-input>
        </el-form-item>
        <el-form-item label="密码：" prop="repassword">
          <el-input v-model="form.repassword" type="repassword" placeholder="请再次输入用户密码"></el-input>
        </el-form-item>
        <el-form-item class="tc">
          <el-button type="primary" @click="register">注册</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { registerAPI } from '@/api'

export default {
  name: 'LoginIndex',
  data () {
    const samePwd = (rule, value, callback) => {
      if (value !== this.form.password) {
        // 如果验证失败，则调用 回调函数时，指定一个 Error 对象。
        callback(new Error('两次输入的密码不一致!'))
      } else {
        // 如果验证成功，则直接调用 callback 回调函数即可。
        callback()
      }
    }
    return {
      form: {
        username: '',
        password: '',
        repassword: ''
      },
      rules: {
        username: [
          {
            required: true,
            message: '请输入用户名',
            trigger: 'blur'
          },
          {
            pattern: /^[a-zA-Z0-9]{1,10}$/,
            message: '用户名必须是1-10的大小写字母数字',
            trigger: 'blur'
          }
        ],
        password: [
          {
            required: true,
            message: '请输入密码',
            trigger: 'blur'
          },
          {
            pattern: /^\S{6,15}$/,
            message: '密码必须是6-15的非空字符',
            trigger: 'blur'
          }
        ],
        repassword: [
          {
            required: true,
            message: '请再次输入密码',
            trigger: 'blur'
          },
          {
            pattern: /^\S{6,15}$/,
            message: '密码必须是6-15的非空字符',
            trigger: 'blur'
          },
          {
            validator: samePwd,
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {

    register () {
      // 进行表单预验证
      this.$refs.regRef.validate(async valid => {
        if (!valid) return false
        // 尝试拿到用户输入的内容
        // console.log(this.regForm)
        // 1. 调用注册接口
        const { data: res } = await registerAPI(this.form)
        // 2. 注册失败，提示用户
        if (res.code !== 0) return this.$message.error(res.message)
        // 3. 注册成功，提示用户
        this.$message.success(res.message)
        // 4. 跳转到登录页面
        this.$router.push('/login')
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.register-page {
  min-height: 100vh;
  background: url(@/assets/images/login_bg.jpg) no-repeat center / cover;
  display: flex;
  align-items: center;
  justify-content: center;

  .register-card {
    width: 540px;
    height: 560px;

    ::v-deep .el-card__header {
      padding-top: 48px;
      height: 80px;
      line-height: 40px;
      height: 140px;
      font-size: 44px;
      text-align: center;
      color: #333;
      border-bottom: none;

      div {
        margin-top: 8px;
        display: block;
        text-decoration-line: none;
        font-size: 16px;
        color: #1E5EFF;
      }
    }
  }

  .el-form {
    margin: 0 auto;
    width: 420px;
    text-align: center;

    .tc {
      text-align: center;
      height: 48px;

      .el-button {
        width: 420px;
      }
    }
  }
}

</style>
